package com.itlong.cloud.controller.property;


import com.itlong.cloud.POJO.DTO.operate.OperateBusinessChargeDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyDeviceAlarmInfoDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateBusinessOperationLogVO;
import com.itlong.cloud.POJO.VO.property.CloudElevatorVO;
import com.itlong.cloud.POJO.VO.property.PropertyDeviceAlarmInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.IotDeviceStatusEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.equip.property.IPropertyDeviceAlarmMessageServiceClient;
import com.itlong.cloud.iottp.IIotServerServiceClient;
import com.itlong.cloud.paymenthub.IOperateProductModelManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.page.PageByVisitor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业—系统消息设备告警模块。
 * <desc/>
 *
 * @createDate 2021-05-31
 */
@RestController
@RequestMapping("project/alarmMessage")
public class PropertyDeviceAlarmMessageController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyDeviceAlarmMessageController.class);

    @Autowired
    IPropertyDeviceAlarmMessageServiceClient iPropertyDeviceAlarmMessageServiceClient;

    @Autowired
    private IOperateProductModelManageServiceClient iOperateProductModelManageServiceClient;

    @Autowired
    IIotServerServiceClient iIotServerServiceClient;

    /**
     * <desc>
     *      实时抓拍
     * <desc/>
     *
     * @param  alarmInfoDTO 告警相关数据
     * @return
     * @author Juguang.S
     * @createDate 2021-05-31
     */
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.DEVICE_ALARM_REAL_TIME_SNAPSHOT_LOG_DESC)
    @RequestMapping(path = "/realTimeSnapshot" , method = RequestMethod.POST)
    public Object realTimeSnapshot(PropertyDeviceAlarmInfoDTO alarmInfoDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            alarmInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyDeviceAlarmInfoDTO.class, request);
            if (alarmInfoDTO==null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            CloudElevatorVO vo = iPropertyDeviceAlarmMessageServiceClient.getElevatorInfoByAlarmId(alarmInfoDTO.getAlarmId());
            if(vo!=null && vo.getStatus()!=1){
                return new MessageVO(PropertyErrorCodeEnum.EQUIP_IS_NOT_ALLOWED_MAKE_ERR.getErrorCode());
            }
            //判断当前设备是否在线，如不在线返回提示
            String isOnline = iIotServerServiceClient.batchGetDeviceState(vo.getIotProductKey(), vo.getDeviceUnique());
            //判断设备是否在线
            if (isOnline == null || (isOnline != null && !isOnline.equals(IotDeviceStatusEnum.ONLINE.getType()))) {
                return new MessageVO(PropertyErrorCodeEnum.CLOUD_ELEVATOR_OUT_LINE.getErrorCode());
            }
            alarmInfoDTO.setIp(HttpProtocolUtil.getIpAddr(request));
            alarmInfoDTO.setType("1");
            iPropertyDeviceAlarmMessageServiceClient.realTimeSnapshotOrCancelAlarm(alarmInfoDTO);
        }catch (Exception e) {
            throw new DataAccessException("【物业_设备告警】实时抓拍失败",e);
        }
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
    }

    /**
     * <desc>
     *      撤消告警
     * <desc/>
     *
     * @param  alarmInfoDTO 告警相关数据
     * @return
     * @author Juguang.S
     * @createDate 2021-05-31
     */
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.DEVICE_ALARM_CANCEL_ALARM_LOG_DESC)
    @RequestMapping(path = "/cancelAlarm" , method = RequestMethod.POST)
    public Object cancelAlarm(PropertyDeviceAlarmInfoDTO alarmInfoDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            alarmInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyDeviceAlarmInfoDTO.class, request);
            if (alarmInfoDTO==null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            CloudElevatorVO vo = iPropertyDeviceAlarmMessageServiceClient.getElevatorInfoByAlarmId(alarmInfoDTO.getAlarmId());
            if(vo!=null && vo.getStatus()!=1){
                return new MessageVO(PropertyErrorCodeEnum.EQUIP_IS_NOT_ALLOWED_MAKE_ERR.getErrorCode());
            }
            //判断当前设备是否在线，如不在线返回失败
            String isOnline = iIotServerServiceClient.batchGetDeviceState(vo.getIotProductKey(), vo.getDeviceUnique());
            //判断设备是否在线
            if (isOnline == null || (isOnline != null && !isOnline.equals(IotDeviceStatusEnum.ONLINE.getType()))) {
                return new MessageVO(PropertyErrorCodeEnum.CLOUD_ELEVATOR_OUT_LINE.getErrorCode());
            }
            alarmInfoDTO.setIp(HttpProtocolUtil.getIpAddr(request));
            alarmInfoDTO.setType("2");
            iPropertyDeviceAlarmMessageServiceClient.realTimeSnapshotOrCancelAlarm(alarmInfoDTO);
        }catch (Exception e) {
            throw new DataAccessException("【物业_设备告警】实时抓拍失败",e);
        }
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
    }

    /**
     * <desc>
     *      获取业务日志列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2021/06/01
     */
    @ResponseBody
    @PostMapping("getLogPage")
    public Object getLogPage(OperateBusinessChargeDTO operateBusinessChargeDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateBusinessChargeDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateBusinessChargeDTO.class,request);
            if (operateBusinessChargeDTO == null || StringUtils.isBlank(operateBusinessChargeDTO.getOuterId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateBusinessOperationLogVO> operateBusinessOperationLogVOPage = iOperateProductModelManageServiceClient.getLogPage(operateBusinessChargeDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), operateBusinessOperationLogVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-系统消息设备告警getLogPage】获取业务日志列表失败",e);
        }
    }

    /**
     * <desc>
     *      获取告警消息列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2021/06/07
     */
    @ResponseBody
    @PostMapping("getPage")
    public Object getPage(PropertyDeviceAlarmInfoDTO alarmInfoDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            alarmInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(PropertyDeviceAlarmInfoDTO.class,request);
            PageByVisitor<PropertyDeviceAlarmInfoVO> deviceAlarmInfoVOPage = iPropertyDeviceAlarmMessageServiceClient.getPage(alarmInfoDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), deviceAlarmInfoVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-系统消息设备告警】获取告警消息列表失败",e);
        }
    }

    /**
     * <desc>
     *      修改设备告警消息的状态。
     * <desc/>
     *
     * @param  messageId 消息ID 多个以逗号隔开
     * @param  type 修改类型 0删除 1修改状态为已读
     * @return
     * @author Juguang.S
     * @createDate 2021/06/07
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.DEVICE_ALARM_UPDATE_STATUS_LOG_DESC)
    @RequestMapping(path = "/updateMessageInfo" , method = RequestMethod.POST)
    public Object updateMessageInfo(String messageId,String type) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            messageId = request.getAttribute("messageId") == null ? null : request.getAttribute("messageId").toString();
            type = request.getAttribute("type") == null ? null : request.getAttribute("type").toString();
            //参数非空校验
            if(StringUtils.isEmpty(messageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyDeviceAlarmMessageServiceClient.updateMessageInfo(messageId,type);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业_修改设备告警消息的状态】修改失败",e);
        }

    }
}
